package com.example.config;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Category;
import com.example.service.CategoryService;
import com.example.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.Arrays;
import java.util.List;

@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {

    ServletContext servletContext;
    @Autowired
    PostService postService;
    @Autowired
    CategoryService categoryService;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Category> categories = categoryService.list(new QueryWrapper<Category>()
                .eq("status", 0));

        servletContext.setAttribute("categorys", categories);

        servletContext.setAttribute("base", servletContext.getContextPath());
        //初始化首页的周评论排行榜

        postService.initIndexWeekRank();
    }
}
